<template>
  <div class="search">
    <!-- 顶部导航 -->
    <van-nav-bar
      title="搜索页"
      left-text="返回"
      right-text="..."
      left-arrow
      @click-left="$router.go(-1)"
    />
    <div class="searchbox">
      <input type="text" placeholder="输入关键词" v-model="searchText" />
      <span @click="search">搜索</span>
    </div>
    <div class="block">
      <div class="title">历史搜索</div>
      <span>
        <button style="margin-left: 250px" @click="del">删除</button>
      </span>
      <div class="list">
        <span v-for="(item, index) in a" :key="index">
          {{ item }}
        </span>
      </div>
    </div>

    <div class="searchFind">
      <div class="find">搜索发现</div>
      <span class="text" v-for="(item, index) in findArr" :key="index">{{
        item
      }}</span>
    </div>
  </div>
</template>

<script>
import { shop_search } from "../axios/api";
export default {
  data() {
    return {
      //负责定义响应式数据
      searchText: "", //保存输入框输入的内容(临时)
      searchArr: [],
      getArr: [],
      findArr: ["三文鱼", "小吃点", "窝窝头"],
      a:[]
    };
  },

  methods: {
    //负责定义方法(函数)
    search() {
      if (this.searchText) {
        // 将输入框中输入的内容 保存到 searchArr 中
        this.searchArr.push(this.searchText);
        this.a =localStorage.setItem("arr", this.searchArr);
        this.searchArr.push(this.a)

        // 清空输入框;
        this.searchText = "";

        //根据输入框的内容查找对应的商品
        shop_search({ searchkey: this.searchArr }).then((res) => {
          this.searchArr = res.data.list;
          // console.log(this.searchArr);
          this.$router.push({
            path: "/searchgoods",
            query: { searchkey: this.searchArr },
          });
        });
      }
    },
    del() {
      this.searchArr = "";
    },
  },

};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.searchbox {
  margin: 10px;
  display: flex;
  align-items: center;
}
.searchbox input {
  flex: 1;
  height: 40px;
  background-color: #f5f5f5;
  outline: none;
  border: none;
  padding-left: 20px;
  border-radius: 20px;
}
.searchbox span {
  width: 60px;
  text-align: center;
}

.block {
  margin: 10px;
}
.block .title {
  font-weight: bold;
  padding: 10px 0;
  display: inline-block;
}
.block .list span {
  display: inline-block;
  background-color: #f5f5f5;
  padding: 5px 10px;
  border-radius: 5px;
  margin-right: 10px;
  margin-bottom: 10px;
}
.searchFind .find {
  font-weight: bold;
  padding: 10px 10px;
}
.searchFind span {
  display: inline-block;
  background-color: #f5f5f5;
  padding: 5px 10px;
  border-radius: 5px;
  margin-right: 10px;
  margin-bottom: 10px;
}
</style>